import java.util.Stack;

/**
 * @program: LeetCode
 * @description: LeetCode :
 * @author: WXY
 * @create: 2023-01-02 15:21
 * @Version 1.0
 **/
public class num020_isValid {
    public static boolean isValid(String s) {
        char[] str = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length; i++) {
            switch (str[i]) {
                case '(':
                    stack.push(')');
                    break;
                case '[':
                    stack.push(']');
                    break;
                case '{':
                    stack.push('}');
                    break;
                default: {
                    if (stack.isEmpty() || str[i] != stack.peek()) {
                        return false;
                    } else {
                        stack.pop();
                        break;
                    }
                }
            }
        }
        return stack.isEmpty();
    }
}
